<!--
 * @Descripttion: 订单管理
 * @version:
 * @Author: 赵致喜
 * @Date: 2020-10-12 18:48:41
 * @LastEditors: 穆宏宇
 * @LastEditTime: 2020-11-26 19:07:10
-->
<template>
	<div class="organization">
		<el-form label-position="'right'" label-width="120px" label-suffix=":">
			<!-- <el-col :span="6">
				<el-form-item label="采购组织">
					<el-select v-model="searchFormData.orgId" clearable filterable @change="changePurchase">
						<el-option :value="item.orgId" :label="item.orgName" v-for="item in purchaseList"></el-option>
					</el-select>
				</el-form-item>
			</el-col> -->
			<el-col :span="6">
				<el-form-item label="采购用户">
					<el-select v-model="searchFormData.purchaseId" clearable filterable>
						<el-option :value="item.purchaseId" :label="item.purchaseName" v-for="item in userList">
						</el-option>
					</el-select>
				</el-form-item>
			</el-col>
			<el-col :span="18">
				<!-- <el-button @click="sendHandle" style="float: right;margin-left: 10px;">新增</el-button> -->
				<el-button @click="refreshControlHandle" style="float: right;margin-left: 10px;margin-bottom: 10px;">重置
				</el-button>
				<el-button @click="searchControlHandle" style="float: right;">查询</el-button>
				<el-button @click="addControlHandle" style="float: right;">停用</el-button>
			</el-col>
		</el-form>
		<div class="table-box">
			<el-table :data="tableData" border @selection-change="handleSelectionChange" height="calc(100vh - 200px)">
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column prop="orgName" label="采购组织"></el-table-column>
				<el-table-column prop="purchaseName" label="采购用户"></el-table-column>
				<!-- <el-table-column prop="discount" label="加价(%)"></el-table-column> -->
				<el-table-column prop="status" label="状态">
					<template slot-scope="scope">
						{{scope.row.status == 0?'启用':''}}
						{{scope.row.status == 1?'停用':''}}
					</template>
				</el-table-column>
				<el-table-column prop="createTime" label="创建时间"></el-table-column>
				<!-- <el-table-column prop="updateTime" label="修改时间"></el-table-column> -->
				<el-table-column label="操作" width="250" fixed="right">
					<template slot-scope="scope">
						<!-- <el-button @click="updateIt(scope.row)" size="mini">修改</el-button> -->
						<el-button @click="deleteIt(scope.row)" size="mini">删除</el-button>
						<!-- <el-button @click="applyPaper(scope.row)" size="mini">申请开进项发票</el-button> -->
					</template>
				</el-table-column>
			</el-table>
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:page-size="pageData.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageData.total"
				style="text-align: right;">
			</el-pagination>

		</div>
		<el-dialog title="停用信息" :visible.sync="paperDialogShow" width="1200px">
			<el-form label-position="'right'" label-width="120px" label-suffix=":">
				<!-- <el-form-item label="采购组织">
					<el-select v-model="paperDict.orgId" clearable filterable ref='myselected' @change="changePurchase">
						<el-option :value="item.orgId" :label="item.orgName" v-for="item in purchaseList"></el-option>
					</el-select>
				</el-form-item> -->
				<el-form-item label="采购用户">
					<el-select v-model="paperDict.purchaseId" clearable filterable ref='myselected'
						@change="getSameList">
						<el-option :value="item.purchaseId" :label="item.purchaseName" v-for="item in userList">
						</el-option>
					</el-select>
				</el-form-item>
				<p style="color:red;margin-bottom:10px;" v-if="!paperDict.id">*标颜色的为已经存在加价信息的用户</p>
				<el-table :data="tableData2" :row-class-name="tableRowClassName" border
					@selection-change="handleSelectionChange2" height="500">
					<el-table-column type="selection" width="50" :selectable="selectable"></el-table-column>
					<el-table-column prop="name" label="姓名"></el-table-column>
				</el-table>
				<!-- <el-pagination @size-change="handleSizeChange2" @current-change="handleCurrentChange2"
					:page-size="pageData2.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageData2.total"
					style="text-align: right;">
				</el-pagination> -->
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitHandle()">停用</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		selOrderDeactivate,
		AddOrderDeactivate,
		UpOrderDeactivate,
		delOrderDeactivate,
		selManagerForPurchase,
		selUserAllUserForDeactivates
	} from '@/api/admin.api'
	import moment from 'moment'
	export default {
		name: 'supplierReconciliation',
		data() {
			return {
				searchFormData: {
					orgId: "",
					purchaseId: "",
					status: 1
				},
				// tableData: [],
				tableData: [],
				tableData2: [],
				// 分页数据
				pageData: {
					// 当前分页
					pageNum: 1,
					// 分页条数
					pageSize: 10,
					// 数据总数
					total: 0,
				},
				pageData2: {
					// 当前分页
					pageNum: 1,
					// 分页条数
					pageSize: 10,
					// 数据总数
					total: 0,
				},
				// 表格选中数据
				tableSelectData: [],
				tableSelectData2: [],
				remarkDialogShow: false,
				paperDialogShow: false,
				remark: "",
				paperDict: {
					discount: '',
					orgId: '',
					orgName: "",
					purchaseId: '',
					purchaseName: ""
				},
				purchaseList: [],
				userList: [],
			}
		},
		methods: {
			selectable(row, index) {
				return row.status == 0 || row.status == null //为true的时候可以选，为false则不可选择
			},
			tableRowClassName({
				row,
				rowIndex
			}) {
				if (row.status == 1) {
					return 'success-warn';
				}
				return '';
			},
			getSameList(e) {
				selUserAllUserForDeactivates({
					purchaseId: e
				}).then((res) => {
					this.tableData2 = res;
				})
			},
			// changeUser(e){
			// 	this.getTableData();
			// },
			changePurchase(e) {
				this.selManagerForPurchase(e);
			},
			selManagerForPurchase(e) {
				selManagerForPurchase({
					orgId: e
				}).then((res) => {
					console.log(res.rows)
					this.userList = res.rows;
				})
			},
			updateIt(row) {
				this.paperDict = row;
				this.paperDialogShow = true;
			},
			deleteIt(row) {
				this.$confirm('是否删除停用模板, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then((data) => {
					delOrderDeactivate({
						id: row.id
					}).then((res) => {
						console.log(res);
						this.$message({
							type: 'success',
							message: '删除成功!'
						});
						this.getTableData();
						this.paperDialogShow = false;
					})
				})

			},
			addControlHandle() {
				this.paperDict = {};
				this.tableData2 = [];
				this.paperDialogShow = true;
			},
			submitHandle(remark) {
				// 这里还要记录相同组织的用户的加价信息，数组拼接  新增和修改
				var that = this;
				var selectArr = this.multipleSelection2;
				if (this.multipleSelection2 == undefined || selectArr.length <= 0) {
					return this.$message({
						type: 'error',
						message: '请选择操作行!'
					});
				}
				var newArr = [];
				selectArr.forEach(item => {
					newArr.push({
						purchaseId: item.id,
						purchaseName: item.name
					})
				})
				if (this.paperDict.id) {
					UpOrderDeactivate(newArr).then((res) => {
						console.log(res);
						this.$message({
							type: 'success',
							message: '修改成功!'
						});
						this.getTableData();
						this.paperDialogShow = false;
					})
				} else {
					// this.paperDict.discount = this.paperDict.discount/100.00
					AddOrderDeactivate(newArr).then((res) => {
						console.log(res);
						this.$message({
							type: 'success',
							message: '停用成功!'
						});
						this.getTableData();
						this.paperDialogShow = false;
					})
				}

			},
			// 检索订单管理列表
			searchControlHandle() {
				this.getTableData()
			},
			handleSizeChange(val) {
				this.pageData.pageSize = val
				this.getTableData()
			},
			handleSizeChange2(val) {
				this.pageData2.pageSize = val
				this.getTableData()
			},
			handleCurrentChange(val) {
				this.pageData.pageNum = val
				this.getTableData()
			},
			handleCurrentChange2(val) {
				this.pageData2.pageNum = val
				this.getTableData()
			},
			handleSelectionChange(val) {
				this.multipleSelection = val;
				console.log(val)
			},
			handleSelectionChange2(val) {
				this.multipleSelection2 = val;
				console.log(val)
			},
			// 重置搜索条件
			refreshControlHandle() {
				this.searchFormData = {
					orgId: ""
				}
				this.getTableData()
			},
			// 请求订单管理列表
			getTableData() {
				const newSearch = {
					...this.searchFormData,
					...this.pageData,
				}
				console.log(newSearch);
				selOrderDeactivate(newSearch).then((res) => {
					console.log(res.rows)
					this.tableData = res.rows;
					this.pageData.total = res.total;
				})
			},
		},
		created() {},
		mounted() {
			// this.selManagerForPurchase();
			this.selManagerForPurchase();
			this.getTableData()
		},
	}
</script>

<style>
	.el-button--text {
		display: none;
	}
</style>
